package edu.cuit.canary.service.bean;

import edu.cuit.canary.dto.OrderDto;
import edu.cuit.canary.feign.client.UserClient;
import edu.cuit.canary.po.Order;
import edu.cuit.canary.mapper.OrderMapper;
import edu.cuit.canary.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceBean implements OrderService {
    @Resource private OrderMapper mapper;
    //@Resource private RestTemplate restTemplate;
    @Resource private UserClient userClient;

    @Override @Transactional
    public Order insertOrder(Order order) {
        mapper.insert(order);
        return order;
    }

    @Override
    public OrderDto findOrderById(Long id) {
        var order = mapper.selectById(id);
        //将order转换为orderDto
        var orderDto = edu.cuit.canary.dto.mapper.OrderMapper.INSTANCE.order2orderDto(order);
        //发起远程调用,根据订单的用户id获取用户信息
        //var url = "http://localhost:8888/users/" + order.getUserId();
        //var url = "http://user-service/users/" + order.getUserId();
        //var user = restTemplate.getForObject(url, User.class);
        var user = userClient.searchUserById(order.getUserId());
        orderDto.setUser(user);
        return orderDto;
    }
}
